<script src="<?php echo base_url()?>js/jquery.min.js" type="text/javascript"></script>
<script src="<?php echo base_url()?>js/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready( function() {
        $(function() {
            $('#loading').ajaxStart(function(){
                $(this).fadeIn();
            }).ajaxStop(function(){
                $(this).fadeOut();
            });

            $('#menu_cm1 a').click(function() {
                var url = $(this).attr('href');
                $('#container2').load(url);
                return false;
            });

           var w = parseInt($('#week_count').val());
            for(var a=0;a<=w;a++){
                $('#click_cm1'+a).click(function(){

                    if ($("#container2").is(":hidden")) {
                        $("#container2").slideDown("slow");
                        //$('#click').text('Tutup');
                    } else {
                        $("#container2").slideUp("slow");

                    }
                });
            }
        });
    });
</script>
<?php $this->load->helper('HTML'); 
echo link_tag('css/style2.css');
echo link_tag('images/favicon.ico','shortcut icon','image/x-icon');
?>
<p>&nbsp;</p>
<h2 class="title">Calon Manager Khusus Satu Bulan</h2>
<div id="loading" style="display:none">Loading...</div>
<form action="<?php echo $form_action?>" method="post" id="frm">
    <table width="90%" border="0" cellpadding="1" cellspacing="1" id="data" align="center">
      <thead>
        <?php if(isset($error)){?>
          <tr>
              <td colspan="5">
                  <?php echo $error?>
              </td>
          </tr>
         <?php }else{ ?>
        <tr  align="center" class="table" >
    <input type="hidden" id="week_count" value="<?php echo count($weeks)?>">
            <th ><?php echo $bulan . " " . $tahun?></th>
            <?php for($i=0;$i<count($weeks);$i++):?>
            <th >Minggu Ke- <?php echo $weeks[$i]->intid_week;?></th>
            <?php endfor;?>
            <th >Total</th>

        </tr>
    </thead>
    <tbody>

        <!-- ============isi ============ -->
        <tr class='data'>
            <td>Penjualan Pribadi</td>
            <?php
			//line ikhlas 
			$count =0;
			//end
            $total=0;
            $total_pv='0.0';
			//line ikhlas 
			$count_pribadi=0;
			$non_syarat_pribadi = 0;
			//end
            for($i=0;$i<count($week_pribadi);$i++):?>
            <td align="center">
                    <?php
                    if(!empty($week_pribadi[$i][0]->total)) {
                        echo $week_pribadi[$i][0]->total;
                        $total = $total + $week_pribadi[$i][0]->total;
						$count = $count +1;
						if($count <= 4){
							if($week_pribadi[$i][0]->total >= 500000){
								$count_pribadi = $count_pribadi + 1;
							}else{
								$non_syarat_pribadi = $non_syarat_pribadi +1;
							}
						}
                    }else {
                        echo "0";
                    }
                    ?>
            </td>
            <?php endfor;?>
            <td align="center">
                <?php //$total = 0;
                echo number_format($total)?>
            </td>
        </tr>

        <tr class="data">
            <td>Rekrut Baru Pribadi</td>
            <?php
			$count =0;
            $total_rekrut=0;
			//line ikhlas
			$count_jumlah = 0;
			$non_syarat_jumlah = 0;
			
            for($i=0;$i<count($week_rekrut);$i++):?>
            <td align="center">
                    <?php
                    if(!empty($week_rekrut[$i][0]->total)) {
                    echo $week_rekrut[$i][0]->total;
                        $total_rekrut = $total_rekrut + $week_rekrut[$i][0]->total;
						//line ikhlas 
						//untuk syarat untuk mengajukan manajer
						$count = $count + 1;
						if($count <= 4){
							if($week_rekrut[$i][0]->total >= 2){
								$count_jumlah = $count_jumlah +1;
							}else{
								$non_syarat_jumlah = $non_syarat_jumlah + 1;
							}
						}
					}else {
                        echo "0";
                    }
                    ?>
            </td>
            <?php endfor;?>
            <td align="center">
                <?php echo $total_rekrut . " Orang"?>
            </td>
        </tr>
        <tr class="data">
            <td>Penjualan Rekrut</td>
            <?php
			$total_jualrekrut=0;
			//line ikhlas
			$count_rekrut = 0;
			$non_syarat_rekrut = 0;
			$count = 0;
            
            for($i=0;$i<count($penjualanrekrut);$i++){?>
            <td align="center">
                    <?php
                    if(!empty($penjualanrekrut[$i])) {
                        $pr = number_format($penjualanrekrut[$i][0]->total);
                        $week = $penjualanrekrut[$i][0]->intid_week;
                        $total_jualrekrut = $total_jualrekrut + $penjualanrekrut[$i][0]->total;
						//line ikhlas count_rekrut
						//untuk penjualan anak rekrut diatas 300ribu
						$count = $count + 1;
						if($count <= 4){
							if($penjualanrekrut[$i][0]->total >= 300000){
								$count_rekrut = $count_rekrut+1;
							}else{
								$non_syarat_rekrut = $non_syarat_rekrut +1;
							}
						}
						?>
                <div id="menu_cm1" style="height: 40px; width: 5px">
                    <?php  echo anchor ('calon_manager/view_rekrut4/'.$strkode."/". $week."/".$bulan_a, ($pr), array('id' =>"click_cm1".$i));?>
                </div>

                        <?php
                    }else {
                        $pr = "0,00";
                        $week = 0;
                        echo $pr;
                    }
                    ?>

            </td>
            <?php }?>

            <td align="center">
                <a href="<?php echo base_url().'calon_manager/extract_pengejaran_anak_langsung?strkode_dealer='.$strkode.'&bulan='.$bulan_a;?>"><?php echo number_format($total_jualrekrut)?></a>
            </td>
        </tr>
		<?php
		
		$total_pv = $week_pv->pv + $week_pv_pribadi->pv;
		?>
		<tr class="data">
            <td>&nbsp;</td>
                <td align="center" colspan="<?php echo count($weeks)?>">Penjualan anak/cucu diluar rekrut baru</td>
                <td align="center">
				<?php if(!empty($penjualan_luar_rekrut_total)){
					$totalpenjualan_luar_rekrut = $penjualan_luar_rekrut_total;
					}else{
							$totalpenjualan_luar_rekrut =0;
					}
					$tsatubulan = $totalpenjualan_luar_rekrut + ($total + $total_jualrekrut);
						if($tsatubulan >= 1600000) {
							$allowSave = '';
						}else {
							$allowSave = 'disabled';
						}
					$total_bener = $tsatubulan - $total - $total_jualrekrut;
				//echo number_format($total_bener);
				/*
				<form method="post" action="<?php echo base_url().'calon_manager/show_excelData'?>">
					<input type="hidden" name="bulan" value="" />
					<input type="hidden" name="strkode_dealer" value="<?php echo $strkode;?>" />
					<input type="submit" value="<?php 	echo number_format($total_bener);?>" />
				</form><br>
				*/
				?>
				<a href="<?php echo base_url().'calon_manager/show_excelData?strkode='.$strkode.'&bulan='.$bulan_a;?>"><?php echo number_format($total_bener);?></a>
			</td>
            </tr>
        <tr class="data">
            <td>&nbsp;</td>
            <td align="center" colspan="<?php echo count($weeks)?>">Total Penjualan Group Pra CM Dalam satu bulan</td>
            <td align="center"><?php 
			
                echo number_format($tsatubulan);?></td>
        </tr>
            <?php
				$nominal = $total_bener +  $total_jualrekrut + $total;
				if($nominal >= 13000000){
					$count_nominal = 1;
				}else{
					$count_nominal = 0;
				}
				if($count_rekrut == 4 and $count_jumlah == 4 and $count_pribadi == 4 and $count_nominal == 1 and $id_cabang == 1){
					?>
					<tr><td><td></tr>
						<tr>
							<td colspan="5" align="left">
								<input type="hidden" name="strkode_dealer"  id="strkode_dealer" value="<?php echo $strkode?>">
								<input type="hidden" name="khusus"  id="khusus" value="<?php echo $khusus?>">
								<input type="hidden" name="total_omset"  id="total_omset" value="<?php echo $tsatubulan?>">
								<input type="hidden" name="total_pv"  id="total_pv" value="<?php echo $total_pv?>">
								<input type="submit" name="simpan" id="submit4" value="Proses"></td>
							<td colspan="2" align="center"></td>
						</tr>						
					<?php
				}else { ?>
            <tr>
                <td colspan="5" align="center"><br>Tidak Memenuhi Syarat ! Belum bisa Melanjutkan ke Calon Manager Berikutnya</td>
            </tr>
            <?php
            }
            ?>
        </tbody>
        <?php } 
		function jum_children ($induk, $level) {
    	$query = "select member.intid_dealer, member.strkode_upline, member.strkode_dealer, member.strnama_dealer, sum(nota.inttotal_omset)as inttotal_omset from member left join nota on nota.intid_dealer = member.intid_dealer where member.strkode_upline='$induk' group by member.intid_dealer";
		$result = $this->db->query($query);
		$l = $level;
		if($result->num_rows() > 0):
			foreach($result->result_array() as $row):
					$child = $this->jum_children($row['strkode_dealer'],$level+1);
					if($child > 1 && $l < $child) $l = $child;	
			endforeach;
		endif;
    	
		
		return $l;
					
	}
	?>
    </table>
</form>
<div id="container2" style="width:400px; height: 100px; display:none;">
		No Display Data
</div>
<div id="message4"></div>
<script type="text/javascript">
    
    $(document).ready( function() {
        $("#rec1").dialog({
		autoOpen: false,
		modal: true,
		width: '300'
	});
    $("#rec2").dialog({
		autoOpen: false,
		modal: true,
		width: '300'
	});
        $('#submit4').click(function() {
            var form_data = {
                strkode_dealer : $('#strkode_dealer').val(),
                khusus : $('#khusus').val(),
                total_omset : $('#total_omset').val(),
                total_pv : $('#total_pv').val(),
                ajax : '1'
            };
            $.ajax({
                url: "<?php echo base_url(); ?>calon_manager/last_selection",
                type: 'POST',
                async : false,
                data: form_data,
                success: function(msg) {
                    $('#message4').html(msg);
                }
            });
            return false;
        });
    });
</script>